package com.simga.library.http;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.Log;

import com.simga.library.utils.SdCardUtil;

import java.io.File;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;

import okhttp3.Cache;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

public final class HttpHelper {

    public static Retrofit mRetrofit;
    public static OkHttpClient mClient;
    public static String host = "http://app.9688app.com/ftzk_test/";
//    public static String host = "https://app.9688app.com/admin/";
    public static HttpHelper apiHttp;

    public static void initHttp(Context context) {
        apiHttp = new HttpHelper(context);
    }

    public HttpHelper(Context context) {
        if (mRetrofit == null) {
            initClient(context);
            mRetrofit = new Retrofit.Builder()
                    .baseUrl(host)
                    .addConverterFactory(FastJsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .client(mClient)
                    .build();
        }
    }

    private void initClient(Context context) {
        if (mClient == null) {
            //设置缓存 10M
            Cache cache;
            try {
                File file = new File(SdCardUtil.CACHE_PATH);
                if (!file.exists())
                    file.mkdirs();
                cache = new Cache(file, 10 * 1024 * 1024);
                HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory(null, null, null);
                mClient = new OkHttpClient.Builder()
                        .retryOnConnectionFailure(true)
                        .addInterceptor(new VerifyInterceptor(getLocalVersionName(context)))
                        .connectTimeout(10, TimeUnit.SECONDS)
                        .writeTimeout(30, TimeUnit.SECONDS)
                        .readTimeout(30, TimeUnit.SECONDS)
                        .cache(cache)
                        .sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager).hostnameVerifier(new HostnameVerifier() {
                            @Override
                            public boolean verify(String hostname, SSLSession session) {
                                return true;
                            }
                        }).build();
            } catch (Exception e) {
                Log.e("OKHttp", "Could not create http cache" + e.getMessage());
            }
        }
    }

    /**
     * 获取本地软件版本号名称
     */
    private String getLocalVersionName(Context ctx) {
        String localVersion = "";
        try {
            PackageInfo packageInfo = ctx.getApplicationContext()
                    .getPackageManager()
                    .getPackageInfo(ctx.getPackageName(), 0);
            localVersion = packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return localVersion;
    }

    public <T> T getService(Class<T> tClass) {
        return mRetrofit.create(tClass);
    }
}
